package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */

@Api(tags = "讲师前端控制器")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation("获取讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("list", list);
    }

    @ApiOperation("根据Id删除讲师")
    @ApiImplicitParam(name="id",value = "讲师id",required = true)
    @DeleteMapping("/{id}")
    public R removeTeacher(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("分页查询讲师列表(不带条件)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "从第几条开始查",required = true),
            @ApiImplicitParam(name = "size",value = "查多少条",required = true)
    })
    @GetMapping("/pageTeacher/{current}/{size}")
    public R pageTeacher(@PathVariable int current,@PathVariable int size){
        Page<EduTeacher> page = new Page<>(current,size);
        teacherService.page(page, null);
        return R.ok().data("total", page.getTotal()).data("item", page.getRecords());
    }

    @ApiOperation("分页条件组合查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "从第几条开始查",required = true),
            @ApiImplicitParam(name = "size",value = "查多少条",required = true)
    })
    @PostMapping("/pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(@PathVariable int current, @PathVariable int size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current, size);
        String name=teacherQuery.getName();
        Integer level=teacherQuery.getLevel();
        String begin=teacherQuery.getBegin();
        String end=teacherQuery.getEnd();

        //条件构造器
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断加条件
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //查询
        teacherService.page(page, wrapper);
        return R.ok().data("total", page.getTotal()).data("list", page.getRecords());
    }


    @ApiOperation("添加讲师")
//    @ApiImplicitParam(name = "teacher",value = "添加的讲师",required = true)
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody(required = false) EduTeacher teacher){
        if (teacherService.save(teacher)){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id查询讲师")
    @ApiImplicitParam(name="id",value = "讲师id",required = true)
    @GetMapping("/{id}")
    public R getTeacher(@PathVariable String id){
        System.out.println(id);
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation("更新讲师信息")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        System.out.println(teacher);
        if (teacherService.updateById(teacher)){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

